<template>
  <el-dialog
    custom-class="upd-car-data"
    v-el-drag-dialog="{ drag: true, full: false, resize: false }"
    title="更新-车辆资料"
    width="900px"
    :visible="true"
    :close-on-click-modal="false"
    @close="closeMd">
    <UpkeepItem :datas="obj" ref="FIR_REPAIR_Ref" type="FIR_REPAIR" name="一级保养" />
    <UpkeepItem :datas="obj" ref="SEC_REPAIR_Ref" type="SEC_REPAIR" name="二级保养" />
    <InsureItem :datas="obj" ref="THIRD_SAFE_Ref" type="THIRD_SAFE" name="第三者险" />
    <InsureItem :datas="obj" ref="CARRIER_SAFE_Ref" type="CARRIER_SAFE" name="承运人险" />
    <InsureItem :datas="obj" ref="COMPULSORY_Ref" type="COMPULSORY" name="交强险" />
    <CardItem :datas="obj" ref="LICENSE_INSPECT_Ref" type="LICENSE_INSPECT" name="行驶证年审" />
    <CardItem :datas="obj" ref="OPERATION_INSPECT_Ref" type="OPERATION_INSPECT" name="营运证年审" />

    <div slot="footer" class="dialog-footer">
      <el-button 
        type="primary" 
        :icon="'el-icon-plus'" 
        @click="onSubmit" 
        :loading="fm.sb.load">
        {{fm.sb.text}}
      </el-button>

      <el-button 
        icon="el-icon-error" 
        @click="closeMd">
        关闭
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import elDragDialog from "@/directive/el-drag-dialog";

import UpkeepItem from './upkeep-item';
import InsureItem from './insure-item';
import CardItem from './card-item';

import * as logisticsSer from "@/api/services/logistics.service";

export default {
  name: "UpdateType",
  directives: {elDragDialog},
  components: {UpkeepItem, InsureItem, CardItem},
  props: {myData: Object},
  data() {
    return {
      fm: {
        fname: "updCarDataFm",
        sb: { load: false, text: "更新" },
      },
      // 车辆对象
      obj: null,
    };
  },
  created() {
    console.log('父级传值', this.myData);
    if (this.myData?.obj) {
      this.obj = this.myData.obj.reminds;
    }
  },
  methods: {
    /* 关闭弹框 */
    closeMd() {
      this.$emit("myClick", {state: false});
    },

    // 提交表单
    async onSubmit() {
      let that = this, U = that.U, sb = that.fm.sb, fg = true;
      
      let validateArr = [], remindInfo = [];
      if(fg){
        let fir = await that.$refs.FIR_REPAIR_Ref.validate();
        let sec = await that.$refs.SEC_REPAIR_Ref.validate();
        let third = await that.$refs.THIRD_SAFE_Ref.validate();
        let carrier = await that.$refs.CARRIER_SAFE_Ref.validate();
        let compulsory = await that.$refs.COMPULSORY_Ref.validate();
        let licenseInspect = await that.$refs.LICENSE_INSPECT_Ref.validate();
        let opertionInspect = await that.$refs.OPERATION_INSPECT_Ref.validate();

        validateArr.push(fir, sec, third, carrier, compulsory, licenseInspect, opertionInspect);
        for (let i = 0; i < validateArr.length; i++) {
          let it = validateArr[i];
          if (it.validate) remindInfo.push(it.data);
        }

        if(remindInfo.length == 0) fg = U.qerror('至少完成一项填写');
      }

      if(fg){
        let params = {
          vehicleId: that.myData.obj.id,
          remindInfo
        }
        sb.load = true;
        logisticsSer.updateVehicle(params).then(res => {
          sb.load = false;

          if (res?.code !== 1) {
            U.qerror(res.msg);
          } else {
            U.qsuccess(res.msg);

            that.closeMd();
            that.$emit("reloadDg");
          }
        });

        setTimeout(() => { sb.load = false; }, 10000);
      }
    },
  },
};
</script>
